var express = require('express');
var mongo = require("mongodb");
var http = require("http");
var host = "localhost";
var port = 27017;
var app = express();
var server = new mongo.Server(host,port,{auto_reconnect: true});//创建数据库所在的服务器服务器
var db = new mongo.Db("node-mongo-examples",server,{safe: true});//创建数据库对象
app.get('/v2/articles', function(req, res){
  res.send('nodejs');
  console.log('success');
  db.open(function (err,db) {//连接数据库
    if(err)
      throw err;
    else{
      console.log("成功建立数据库连接");
    }
    //存入数据库
    db.collection('news',function(error,collection){
      collection.find({name: 'news',type: {id: 1}
      }).toArray(function(err,doc){
        console.log(doc);
        console.log('成功查询数据库主页新闻');
      })
    });
    //db.close();
  });
});
app.listen(port,host);




